var NewUser = {
    ib_username: document.getElementById('ib_text_nametocheck'),
    ib_email: document.getElementById('ib_text_emailtocheck'),
    ib_text_password_password: document.getElementById('ib_text_password_password'),
    ib_text_password_confirm: document.getElementById('ib_text_password_confirm'),
    img_password_status: document.getElementById('img_password_status'),
    img_username_status: document.getElementById('img_username_status'),
    img_email_status: document.getElementById('img_email_status'),
    ib_submit_status: document.getElementById('ib_submit_status'),
    err_check_username: document.getElementById('err_check_username'),
    err_check_email: document.getElementById('err_check_email'),
    username_check_wait: document.getElementById('username_check_wait'),
    submit_button: document.getElementById('ib_submitbutton'),
    
    form_complete: document.getElementById('form_complete'),
    signup_form: document.getElementById('signup_form'),
    
    init: function() {
	NewUser.updateSubmitButton();
    },
    
    checkUsername: function() {
	EMDE.showElement(NewUser.username_check_wait);
	value = NewUser.ib_username.value;
	if(value.toString().length == 0) {
	    EMDE.hideElement(NewUser.username_check_wait);
	    NewUser.img_username_status.src = '/img/not_ok.png';
	} else {
	    if(value.toString().length < 5) {
		NewUser.checkUsername_callback(false, 'Username is too short. It must be at least 5 characters long.');
		return false;
	    } else if(value.toString().length > 14) {
		NewUser.checkUsername_callback(false, 'Username is too long. It must be at most 14 characters long.');
		return false;
	    }
	    if(NewUser.usernameValidate(value) == false) {
		NewUser.checkUsername_callback(false, 'A username can only be alphanumeric (underscores are allowed), with at least one letter.');
		return false;
	    }
	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/' + 'checkuser/' + value);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	    function ol_Add(_emde, cb_ele) {
		_emde.hideElement(cb_ele);
	    }
	    frame.onload = ol_Add(EMDE, NewUser.username_check_wait);
	}
	return true;
    },
    
    checkUsername_callback: function(boolValid, errorMsg) {
	if(boolValid == true) {
	    NewUser.img_username_status.src = '/img/ok.png';
	    EMDE.hideElement(err_check_username);
	} else {
	    NewUser.img_username_status.src = '/img/not_ok.png';
	    NewUser.err_check_username.innerHTML = errorMsg;
	    EMDE.showElement(err_check_username);
	}
	NewUser.updateSubmitButton();
    },
    
    checkPassword: function() {
	if(NewUser.ib_text_password_confirm.value == NewUser.ib_text_password_password.value && NewUser.ib_text_password_password.value.toString().length > 0) {
	    NewUser.img_password_status.src = '/img/ok.png';
	} else {
	    NewUser.img_password_status.src = '/img/not_ok.png';
	}
	NewUser.updateSubmitButton();
    },
    
    checkEmail: function() {
	var email = NewUser.ib_email.value;
	/*var regex = new RegExp(/^[a-zA-Z0-9_-]+[@][a-zA-Z0-9_-]+[.][a-zA-Z0-9]+/);
	var valid = regex.exec(email);*/
	if(/*valid == null*/email.indexOf("@") == -1 || email.indexOf('.') == -1) {
	    EMDE.showElement(NewUser.err_check_email);
	    NewUser.img_email_status.src = '/img/not_ok.png';
	} else {
	    EMDE.hideElement(NewUser.err_check_email);
	    NewUser.img_email_status.src = '/img/ok.png';
	}
	NewUser.updateSubmitButton();
    },
    
    usernameValidate: function(username) {
	var regex = new RegExp(/^[a-zA-Z0-9_]+[a-zA-Z][a-zA-Z0-9_]+$/);
	var valid = regex.exec(username);
	if(valid == null) {
	    return false;
	} else {
	    return true;
	}
    },
    
    updateSubmitButton: function() {
	var oksrc = '/img/ok.png';
	if(NewUser.img_password_status.src.toString().indexOf(oksrc) == -1 || NewUser.img_email_status.src.toString().indexOf(oksrc) == -1 || NewUser.img_username_status.src.toString().indexOf(oksrc) == -1) {
	    NewUser.submit_button.disabled = true;
	} else {
	    NewUser.submit_button.disabled = false;
	}
    },
    
    SubmitCallback: function(boolValid, errorMsg) {
	if(boolValid == true) {
	    EMDE.hideElement(NewUser.ib_submit_status);
	    EMDE.hideElement(NewUser.signup_form);
	    EMDE.showElement(NewUser.form_complete);
	} else {
	    NewUser.ib_submit_status.innerHTML = errorMsg;
	    EMDE.showElement(NewUser.ib_submit_status);
	}
    }
}
NewUser.init();